﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GtsServer.Service.Model;
using GtsServer.DAL;
using System.Data;

namespace GtsServer.Service.Controller
{
    public class InviteController
    {
        UserDAL dal = new UserDAL();
        /// <summary>
        /// 推荐信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public InviteMsgResponse InviteMsg(InviteMsgRequest request)
        {
            InviteMsgResponse response = new InviteMsgResponse()
            {
                succeed = "",
                score = "",
                coin = "",
                message = "",
                regulation = ""
            };

            string userAccount = BaseController.Account(request.info);

            if (string.IsNullOrEmpty(userAccount))
                return response;

            DataSet ds = dal.GetModelByUserAccount(userAccount);

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                response.score = "当前邀请积分：" + dr["InviteScore"];
                response.succeed = "邀请成功数：" + dr["InviteSuccessNum"];
                response.coin = "可领取的金币：" + (Convert.ToInt32(dr["InviteScore"]) / 5);
                
                response.regulation = "1.邀请有什么好处？\n";
                response.regulation += "当您成功邀请朋友后，您就可以获得邀请积分，用积分就可免费兑换金币啦。此外，您邀请的朋友在游戏中产生支付的金额也会转换成您的积分哦。\n";
                response.regulation += "您成功邀请的朋友越多，您的积分也越多哦！\n\n";
                response.regulation += "2.如何邀请朋友？\n";
                response.regulation += "点击邀请界面的【邀请朋友赚金币】按钮，会跳至短信界面。将短信中的内容发给您的朋友。\n";
                response.regulation += "如果您的朋友收到短信，通过短信中的下载地址下载并安装游戏包。\n";
                response.regulation += "您的朋友在完成安装进入游戏后，在邀请界面点击【输入邀请码】，输入短信中的数字邀请码，即完成邀请。\n";
                response.regulation += "如果您是被邀请者，则输入邀请码后，您也可以获得额外的金币奖励。\n\n";
                response.regulation += "3.如何算邀请成功？\n";
                response.regulation += "如果您邀请的朋友完成邀请码的输入后，并在游戏中获得1000以上彩蛋，则算成功邀请。您可以自动获得200积分。\n";
                response.regulation += "如果您邀请的多个朋友都满足以上条件，您的积分也会累加。\n\n";
                response.regulation += "4.如何获得更多的邀请积分？\n";
                response.regulation += "除了成功邀请朋友之外。如果您邀请的朋友在游戏中产生充值，则每支付1元您可以获得150积分。\n";
                response.regulation += "您邀请的朋友支付的金额越多，您可以获得的积分也越多。\n\n";
                response.regulation += "5.如何获得金币？\n";
                response.regulation += "您的邀请积分总额除以5即为您可以获得的金币。点击【领取金币】按钮后，可以在【我的物品】栏中领取金币。\n";
                response.regulation += "当领取金币后会扣除相应的邀请积分。";

                response.message = "我正在玩欢乐水果机（兑奖版），可赢取各种实物奖品哦。推荐码：" + userAccount + "，下载地址：http://version.gtscns.cn:3330/Content/Version/InviteVersion/FruitMachine.apk";
            }

            return response;
        }

        /// <summary>
        /// 推荐提取金币
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public InviteGetCoinResponse InviteGetCoin(InviteGetCoinRequest request)
        {
            InviteGetCoinResponse response = new InviteGetCoinResponse()
            {
                result = 0
            };

            string userAccount = BaseController.Account(request.info);

            if (string.IsNullOrEmpty(userAccount))
                return response;

            DataSet ds = dal.InviteGetCoin(userAccount);

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                response.result = Convert.ToInt32(ds.Tables[0].Rows[0]["result"]);
            }
            return response;
        }

        /// <summary>
        /// 验证推荐码
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public InviteVerifReponse InviteVerif(InviteVerifRequest request)
        {
            InviteVerifReponse response = new InviteVerifReponse()
            {
                result = 0
            };

            string userAccount = BaseController.Account(request.info);
            string inviteAccount = request.verification.id;

            if (string.IsNullOrEmpty(userAccount) || string.IsNullOrEmpty(inviteAccount))
                return response;

            DataSet ds = dal.InviteVerif(userAccount, inviteAccount);

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                response.result = Convert.ToInt32(ds.Tables[0].Rows[0]["result"]);
            }
            return response;
        }
    }
}
